package com.ymttest.business.data;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ymatou.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.ymatou.iapi.couponmanage.service.SellerCreateBatchCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productquery.parameter.ProductDetail.ProductInDetailBean;
import com.ymatou.iapi.productquery.service.GetProductInfoByProductIdCall;
import com.ymatou.iapi.selleractivity.service.ApplyActivityCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymttest.business.service.AdminCallService;
import com.ymttest.business.service.AuthCallServiceV2;
import com.ymttest.business.service.CouponServiceMCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductQueryCallService;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

public class Data201812 {
	/**
	 * 创建商家券
	 * @param sellerId
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String createSellerCoupon(Integer sellerId) throws ClientProtocolException, IOException {
		// 创建优惠券
					CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();

					SellerCreateBatchBean sellerCreateBatchBean = CouponServiceMCallServiceV2
							.setDefaultSellerCreateBatchBean();

					sellerCreateBatchBean.setCouponTotalCount(9999999);
					sellerCreateBatchBean.setOrderMinAmount(new BigDecimal(1));
					sellerCreateBatchBean.setSellerId(sellerId);
					sellerCreateBatchBean.setCouponAmount(new BigDecimal(0.01));
					sellerCreateBatchBean.setReceiveNumPerUser(999);
					sellerCreateBatchBean.setAcquireEndTime(YMTDateUtil.getDateBeforeOrNextSecond(60*60*24*50));
					sellerCreateBatchBean.setEffectiveEndDate(YMTDateUtil.getDateBeforeOrNextSecond(60*60*24*50));
					SellerCreateBatchCall sellerCreateBatchCall = CouponServiceMCallServiceV2
							.SellerCreateBatch(sellerCreateBatchBean);
					String batchCode = sellerCreateBatchCall.getBatchCode();
					return batchCode;
	}
	/**
	 * 重新创建token
	 * @param args
	 * @throws Exception
	 */
	public static void rebuildToken() {
		TestDataManager.getLines("uid.txt").forEach(uid->{
			 try {
				AuthCallServiceV2.createToken(Integer.valueOf(uid), "app","app_"+Integer.valueOf(uid));
			} catch (NumberFormatException | IOException e) {
				e.printStackTrace();
			}
		 });
	}
	/**
	 * stg重新生成商家token
	 */
	public static void rebuildSellerToken() {
		TestDataManager.appendFileToLine("sellertoken.txt ", "sellerid,token");
		TestDataManager.getLines("data").forEach(sellerline->{
			String[] sellerinfo = sellerline.split(" ");
			Integer userid = Integer.valueOf(sellerinfo[0]);
			String uname = sellerinfo[1];
			 try {
				String token = AuthCallServiceV2.createToken(userid, "sellerapp","seller"+userid);
				TestDataManager.appendFileToLine("sellertoken.txt ", userid+","+token);
			} catch (NumberFormatException | IOException e) {
				e.printStackTrace();
			}
		 });
	}
	/**
	 * stg批量上架
	 */
	public static void batchSetProductOnSale() {
		//36: private static String logfile = "stgcreateproduct.log";  
		TestDataManager.getLines("stgcreateproduct.log").forEach(info->{
			 try {
				 String[] s = info.split(":");
				 if(s.length>2) {
					 Integer sellerid=Integer.valueOf(s[0]);
					 String pid=s[1];
					 ProductManagerCallServiceV2.batchSetProductOnSale(Arrays.asList(pid), sellerid);
				 }
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 });
	}
	/**
	 * 批量上架
	 * @param pids
	 * @param sellerid
	 */
	public static void batchSetProductOnSale(List<String> pids,Integer sellerid) {
		//36: private static String logfile = "stgcreateproduct.log";  
		
			 try {
				 ProductManagerCallServiceV2.batchSetProductOnSale(pids, sellerid);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	/**
	 * 查询商品规格
	 * @param productId
	 * @return
	 */
	public static List<String> getCatalogList(String productId) {
		JSONObject prodDetail = new GetProductInfoByProductIdCall().callAndGetReturnData(productId);

		ProductInDetailBean prodBean = null;
		List<String> catalogList = new ArrayList<>() ;
		try {
			prodBean = ProductQueryCallService.getBeanFromJson(prodDetail.getJSONObject("Data").getJSONObject("Product"), ProductInDetailBean.class);
			catalogList=prodBean.getCatalogList().stream().map(x -> x.getCatalogId()).collect(Collectors.toList());
		} catch (Throwable e) {
			try {
				Thread.sleep(1000);
				prodBean = ProductQueryCallService.getBeanFromJson(prodDetail.getJSONObject("Data").getJSONObject("Product"), ProductInDetailBean.class);
				catalogList=prodBean.getCatalogList().stream().map(x -> x.getCatalogId()).collect(Collectors.toList());
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		return catalogList;
	}
	public static void main(String[] args) throws Exception {
//		for (int i = 0; i < 10; i++) {
//			AddSellerProductRequest addpro =  ProductManagerCallServiceV2.initAddSellerProductRequest(20224936);
//	        //获取商品ID，单规格
//	        String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
//	        Logger.debug(productId);
//		}
		
//		List<String> pids=Arrays.asList("p5147931","p5147932","p5148328","p5147933","p5148329","p5148330","p5147934","p5147935","p5148331","p5148332","p5147936");
		List<String> list = TestDataManager.getLines("stgcreateproduct.log");
		list.forEach(line->{
			String[] ss = line.split(":");
			Data201812.batchSetProductOnSale(Arrays.asList(ss[1]),Integer.valueOf( ss[0]));
		});
		
//		Data201812.rebuildSellerToken();
		
//		List<String> list = TestDataManager.getLines("data2");
//
//		for (String datas : list) {
//			Integer sellerId = Integer.valueOf(datas.split(" ")[0]);
//			Logger.debug(Data201812.createSellerCoupon(sellerId));
//		}
//		AuthCallServiceV2.createToken(13806220, "app","app_"+13806220);
	}
}
